﻿using System.Web;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.EntityModel;

namespace MonkeyWarranty.Web.Common
{
    public class WarrantyCommon
    {
        private static InstantTicketBiz _instantTicketBiz;
        private static InstantTicketBiz InstantTicketManager
        {
            get { return _instantTicketBiz ?? (_instantTicketBiz = new InstantTicketBiz()); }
        }

        private static readonly object Locker = new object();
        private static long _instantTicketIdentity;
        public static long InstantTicketNextCode
        {
            get
            {
                lock (Locker)
                {
                    if (_instantTicketIdentity == 0)
                    {
                        _instantTicketIdentity = InstantTicketManager.GetNextCode();
                    }
                    else
                    {
                        _instantTicketIdentity++;
                    }
                }
                return _instantTicketIdentity;
            }
        }

        private static WarrantyTicketBiz _warrantyTicketBiz;
        private static WarrantyTicketBiz WarrantyTicketManager
        {
            get { return _warrantyTicketBiz ?? (_warrantyTicketBiz = new WarrantyTicketBiz()); }
        }

        private static readonly object Locker1 = new object();
        private static long _warrantyTicketIdentity;
        public static long WarrantyTicketNextCode
        {
            get
            {
                lock (Locker1)
                {
                    if (_warrantyTicketIdentity == 0)
                    {
                        _warrantyTicketIdentity = WarrantyTicketManager.GetNextCode();
                    }
                    else
                    {
                        _warrantyTicketIdentity++;
                    }
                }
                return _warrantyTicketIdentity;
            }
        }

        private static Employee _employee;
        public static Employee CurrrentEmployee
        {
            get
            {
                _employee = (Employee)HttpContext.Current.Session["CurrentEmployee"];
                if (_employee == null)
                {
                    var employeeBiz = new EmployeeBiz();
                    if (HttpContext.Current.User != null)
                    {
                        _employee = employeeBiz.GetByName(HttpContext.Current.User.Identity.Name);
                        //_employee.Roles = employeeBiz.GetRolesOfEmployee(_employee.Id);
                    }

                    HttpContext.Current.Session["CurrentEmployee"] = _employee;
                }
                return _employee;
            }

            set
            {
                if (value == null)
                {
                    HttpContext.Current.User = null;
                    HttpContext.Current.Session.Remove("CurrentEmployee");
                    return;
                }
                HttpContext.Current.Session["CurrentEmployee"] = value;
            }
        }

        public static bool HasRole(string roleName)
        {
            return CurrrentEmployee.HasRole(roleName);
        }

        public static string GetGlobalString(string key)
        {
            return (string)HttpContext.GetGlobalResourceObject("GlobalStrings", key);
        }
    }
}